package com.suke.czx.modules.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suke.czx.modules.sys.mapper.SysUserRoleMapper;
import com.suke.czx.modules.sys.entity.SysUserRole;
import com.suke.czx.modules.sys.service.SysUserRoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 应用模块名称: 用户角色
 * 代码描述: 用户角色业务层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/08/27 16:31
 */
@Service
@AllArgsConstructor
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    /**
     * 全删全加用户角色关系
     * @param userId 用户id
     * @param roleIdList 角色id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void fullUpdateUserRoleList(Long userId, List<Long> roleIdList) {
        baseMapper.delete(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId, userId));
        if(CollUtil.isNotEmpty(roleIdList)) {
            roleIdList.forEach(roleId -> {
                SysUserRole userRole = new SysUserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(roleId);
                baseMapper.insert(userRole);
            });
        }
    }

}
